package kr.lgh.sms_creditcard.Setting;

import java.util.ArrayList;

import kr.lgh.sms_creditcard.R;
import kr.lgh.sms_creditcard.DB.SQLiteDBHelper;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CategoryList extends Activity {
	ListView listview;
	SQLiteDBHelper dbHelper;
	SQLiteDatabase db;
	Cursor cursor;
	ActionBar actionBar;
	MyCustomAdapter Adapter;
	ArrayList<CategoryInfo> arCategory;
	ArrayAdapter<String> adapter;

	final static String sql = "select * from Category_Table";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.categorytype);

		actionBar = getActionBar();
		actionBar.show();

		dbHelper = new SQLiteDBHelper(this);
		db = dbHelper.getWritableDatabase();
		arCategory = new ArrayList<CategoryInfo>();

		cursor = db.rawQuery(sql, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			CategoryInfo info = new CategoryInfo();
			info.setId(cursor.getInt(0));
			info.setName(cursor.getString(1));

			arCategory.add(info);
			cursor.moveToNext();
		}

		listview = (ListView) findViewById(R.id.categorylist);
		Adapter = new MyCustomAdapter(this, arCategory);
		listview.setAdapter(Adapter);

	}

	// CustomAdapter 생성
	private class MyCustomAdapter extends BaseAdapter {

		Context context;
		LayoutInflater inflater;
		ArrayList<CategoryInfo> lists = new ArrayList<CategoryInfo>();
		TextView category_name;

		public MyCustomAdapter(Context context, ArrayList<CategoryInfo> lists) {
			inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.context = context;
			this.lists = lists;
		}

		@Override
		public int getCount() {
			return lists.size();
		}

		@Override
		public Object getItem(int position) {
			return lists.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final int selectPos = position;
			final int id = arCategory.get(selectPos).getId();

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.row_category, parent,
						false);
			}
			category_name = (TextView) convertView
					.findViewById(R.id.category_name);
			category_name.setText(lists.get(position).getName());

			Button btn_modify = (Button) convertView
					.findViewById(R.id.btn_modify);
			Button btn_delete = (Button) convertView
					.findViewById(R.id.btn_delete);

			btn_modify.setOnClickListener(new Button.OnClickListener() {

				@Override
				public void onClick(View v) {
					TextView tv = (TextView) v;
					Log.d("TAG", selectPos + "번쨰 카테고리");

					final LinearLayout linear = (LinearLayout) View.inflate(
							CategoryList.this, R.layout.input_category, null);
					EditText edit = (EditText) linear
							.findViewById(R.id.input_category);
					edit.setText(arCategory.get(selectPos).getName());
					edit.selectAll();

					new AlertDialog.Builder(CategoryList.this)
							.setTitle("새로운 카테고리를 입력하세오")
							.setView(linear)
							.setPositiveButton("확인",
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {

											CategoryInfo info = new CategoryInfo();
											EditText edit = (EditText) linear
													.findViewById(R.id.input_category);
											Log.d("TAG", "id : " + id
													+ "selectPos : "
													+ selectPos);
											dbHelper.modify_CategoryData(edit
													.getText().toString(), id);
											info.setId(id);
											info.setName(edit.getText()
													.toString());
											arCategory.set(selectPos, info);
											Adapter.notifyDataSetChanged();

										}
									}).setNegativeButton("취소", null).show();
				}
			});

			btn_delete.setOnClickListener(new Button.OnClickListener() {

				@Override
				public void onClick(View v) {

					AlertDialog.Builder configureDlg = new AlertDialog.Builder(
							CategoryList.this);
					configureDlg
							.setTitle("정말로 삭제하시겠습니까?")
							.setIcon(android.R.drawable.ic_menu_edit)
							.setPositiveButton("예",
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											dbHelper.delete_CategoryData(id);
											arCategory.remove(selectPos);
											Adapter.notifyDataSetChanged();

										}
									}).setNegativeButton("아니오", null).show();

				}
			});

			return convertView;
		}

	} // end of MyCustonAdapter

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.menu, menu);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {

		case R.id.item_01:
			final LinearLayout linear = (LinearLayout) View.inflate(this,
					R.layout.input_category, null);

			new AlertDialog.Builder(this)
					.setTitle("카테고리 추가")
					.setIcon(android.R.drawable.ic_input_add)
					.setView(linear)
					.setPositiveButton("확인",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									CategoryInfo info = new CategoryInfo();
									final int final_id = arCategory.get(
											arCategory.size() - 1).getId();
									EditText edit = (EditText) linear
											.findViewById(R.id.input_category);
									info.setId(final_id + 1);
									info.setName(edit.getText().toString());

									dbHelper.insert_CategoryData(info);
									arCategory.add(info);
									Adapter.notifyDataSetChanged();
								}
							}).setNegativeButton("취소", null).show();
			return true;
		}
		return false;
	}

}
